﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FileSystem.Services.IServices;
using FileSystem.Services.Realization;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace FileSystem.Views {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SettingPage : ContentPage {
        public SettingPage() {
            InitializeComponent();
        }

        private async void LearMoreFirstButton_OnTapped(object sender,
            EventArgs e) {
            await Browser.OpenAsync("http://faculty.neu.edu.cn/zhangyin/");
        }

        private async void LearMoreSecondButton_OnTapped(object sender,
            EventArgs e) {
            await Browser.OpenAsync("http://faculty.neu.edu.cn/cse/fengshi/");
        }

        private async void ProjectWebPage_OnTapped(object sender, EventArgs e) {
            await Browser.OpenAsync("https://gitee.com/li-jie1/FileSystem");
        }

        private async void Button_OnClicked(object sender, EventArgs e) {
            UserStorage userStorage=new UserStorage(new PreferenceStorage());
            RootDirItemStorage rootDirItemStorage=new RootDirItemStorage(new PreferenceStorage());
            DirItemStorage dirItemStorage=new DirItemStorage(new PreferenceStorage());
            PhysicalDiskBlockStorage physicalDiskBlockStorage=new PhysicalDiskBlockStorage(new PreferenceStorage());
            DiskBlockStorage diskBlockStorage=new DiskBlockStorage(new PreferenceStorage());
            AccessControlInfoStorage accessControlInfoStorage=new AccessControlInfoStorage(new PreferenceStorage());

            await userStorage.CloseAsync();
            File.Delete(UserStorage.DbPath);
            await userStorage.InitializeAsync();

            await rootDirItemStorage.CloseAsync();
            File.Delete(RootDirItemStorage.DbPath);
            await rootDirItemStorage.InitializeAsync();

            await dirItemStorage.CloseAsync();
            File.Delete(DirItemStorage.DbPath);
            await dirItemStorage.InitializeAsync();

            await physicalDiskBlockStorage.CloseAsync();
            File.Delete(PhysicalDiskBlockStorage.DbPath);
            await physicalDiskBlockStorage.InitializeAsync();

            await diskBlockStorage.CloseAsync();
            File.Delete(DiskBlockStorage.DbPath);
            await diskBlockStorage.InitializeAsync();

            await accessControlInfoStorage.CloseAsync();
            File.Delete(AccessControlInfoStorage.DbPath);
            await accessControlInfoStorage.InitializeAsync();

            await this.DisplayAlert("提示", "磁盘格式化成功!", "确定");
        }
    }
}